<?php
header("Content-Type: text/html; charset=iso-8859-1");
class info{
	
	var $fields;
	
	function info($id = ""){
		$fields = $this->fields();
		if($id){
			$q = "select * from infos where id = '$id'";
			$r = ExecRequete($q);
			if(!mysql_num_rows($r)){
				$this->id = $id;
			}
			else{
				$s = LigneSuivante($r);
				foreach($fields as $field => $type){
					$this->$field = str_replace(chr(146),chr(39),$s->$field);
				}
			}				
		}
		else{
			foreach($fields as $field => $type){
				$this->$field = "";
			}
		}
	}
	function fields(){
		$fields = describe('infos');
		return $fields;
	}
	function list_commentaires(){
		$q = "select membre_id,concat(prenom,' ',nom) as membre, contenu, date_format(datetime,'%d.%m.%Y %H:%i') as datetime, commentaires_info.id as id from commentaires_info left join membres on membre_id = membres.id where info_id = '$this->id' order by datetime asc";
		$r = ExecRequete($q);
		while($s = LigneSuivante($r)){
			$icons = ($s->membre_id == check_cookie() || check_admin()) ? "<img src = 'images/icon_edit.png' width = '8' height = '8' onclick = 'edit_comment($this->id,$s->id)' style = 'margin-left: 3px; cursor: pointer; vertical-align: middle;' title = 'modifier'/><img src = 'images/icon_delete.png' width = '8' height = '8' onclick = 'del_comment($s->id)' style = ' vertical-align: middle; cursor: pointer;' title = 'effacer'/>" : "";
			echo box($s->membre."<span class = 'info_date'>".$s->datetime."$icons</span>","<div id ='commentaire_$s->id'>".$s->contenu."</div>",'690','smallinfo');
		}
	}
	
	function nb_commentaires(){
		$q = "select count(*) as nb from commentaires_info where info_id = '$this->id'";
		$r = ExecRequete($q);
		if($s = LigneSuivante($r)){
			return $s->nb;
		}
		else return 0;
	}
	
	function get_commentaire($id){
		$q = "select contenu, membre_id from commentaires_info where commentaires_info.id = $id";
		$r = ExecRequete($q);
		$return = "";
		if($s = LigneSuivante($r)){
			if($s->membre_id == check_cookie || check_admin()) $return = $s->contenu;
		}
		return $return;
	}
	
	function insert(){
		$q = "insert into infos (valide) values (0)";
		$r = ExecRequete($q);
		$id = mysql_insert_id();
		$this->id = $id;
		if($this->membre_id && $this->corps && $this->titre){
			$rand = (ereg("img:(tmp_[0-9]*):",$this->corps,$regs)) ? $regs[1] : "";
			$this->corps = preg_replace("/img:(tmp_[0-9]*):/","img:$this->id:",$this->corps);
			$this->datetime = "now()";
			$texte = ($this->valide) ? "info publi&eacute;e" : "info mise en attente de publication";
			if($this->update()){
				if($rand){
					rename("images/infos/$rand","images/infos/$this->id");
				} 
			}  
			return $texte;
		}
		else return "Soit le contenu, soit le titre est manquant";
	}
	function update(){
		$fields = $this->fields();
		$q = "replace infos set ";
		foreach($fields as $field => $type){
				if($this->$field == 'now()') $q .= $field." = ".$this->$field.", ";
				elseif(!get_magic_quotes_gpc()) $q .= $field." = '".(addslashes($this->$field))."', ";
				else $q .= $field." = '".$this->$field."', ";
		}
		if(strstr($q,",") !== false){
			$q = substr($q,0,-2);
			if(ExecRequete($q)) return 1;
		}
		else return 0;			
	}
	
	function delete(){
		if(is_dir("images/infos/$this->id")) rmdirtree("images/infos/$this->id");
		$q = "delete from infos where id = '$this->id' limit 1";
		if(ExecRequete($q)) return 1;
		else return 0;
	}
}
?>
